/**
 * 
 */
package ufpr.mestrado.ais.problems.singleObjective;

import jmetal.base.Problem;
import jmetal.base.Solution;
import jmetal.base.solutionType.RealSolutionType;
import jmetal.util.JMException;
import ufpr.mestrado.ais.util.Complex;

/**
 * @author gabriel
 * 
 */
public class Raiz extends Problem {

	public Raiz(String solutionType) throws ClassNotFoundException {
		numberOfVariables_ = 2;
		numberOfObjectives_ = 1;
		numberOfConstraints_ = 0;
		problemName_ = "Raiz";

		upperLimit_ = new double[numberOfVariables_];
		lowerLimit_ = new double[numberOfVariables_];
		for (int var = 0; var < numberOfVariables_; var++) {
			lowerLimit_[var] = -2.0;
			upperLimit_[var] = 2.0;
		}

		if (solutionType.compareTo("Real") == 0)
			solutionType_ = new RealSolutionType(this);
		else {
			System.out.println("Error: solution type " + solutionType
					+ " invalid");
			System.exit(-1);
		}
	}

	@Override
	public void evaluate(Solution solution) throws JMException {

		double x = solution.getDecisionVariables()[0].getValue();

		double y = solution.getDecisionVariables()[1].getValue();

		Complex z1 = new Complex(1, 0);
		Complex z2 = new Complex(x, y);
		Complex z = new Complex(x, y);
		for (int i = 0; i <= 4; i++) {

			z2 = z2.times(z);
		}
		z2 = z2.minus(z1);

		final double value = 1.0 / (1.0 + z2.mod());

		solution.setObjective(0, value);

		solution.setFitness(value);

	}

}
